﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
public partial class admin_PersonalInfo : System.Web.UI.UserControl
{
#region "Member variables"
    int intLanguageID;
    int intUserID;
    DANguoidung objNguoidung;
    DALog objLog;
#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion     
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objNguoidung = new DANguoidung(sqlConnec);
        objLog = new DALog(sqlConnec);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
        loadDataAccess();
        if (!Page.IsPostBack)
        {
            LoadUser(intUserID);
            string strDelete = "";
            strDelete = "Bạn chắc chắn muốn thay đổi OK , Bạn hủy lệnh CANCEL ";
            btnInsert.Attributes.Add("Onclick", "javascript:if(confirm('" + strDelete + "')) {return true} else {return false};");
            objLog.insertLog(0, 0, 5, intUserID);
        }
    }
    private void LoadUser(int intUserID)
	{
        DataRow drUser = objNguoidung.loadNguoidungID(intUserID);
        int intUser = Convert.ToInt32(drUser["ID"].ToString().Trim());
        this.txtUserName.Text = drUser["TenDangNhap"].ToString().Trim();
        this.txtFullName.Text = drUser["HoTen"].ToString().Trim();
	}
	private void UpdateUserAccount()
	{
		if (Page.IsValid)
		{
            string strUserName = this.txtUserName.Text.Trim();
            string strPassWord = this.txtPassWord.Text.Trim();
            string strRePassWord = this.txtRePassWord.Text.Trim();
            string FullName = this.txtFullName.Text;
            if (strPassWord.Equals(""))
			{
				this.ctvPassword.IsValid = false;
			}
			else if(strRePassWord.Equals(""))
			{
				this.ctvRePassword.IsValid = false;
			}
			else
			{
				strPassWord = FormsAuthentication.HashPasswordForStoringInConfigFile(strPassWord, "SHA1");
                int intChẹck = objNguoidung.updateNguoidung(intUserID, strUserName, strPassWord, FullName);
				if(intChẹck > 0)
				{
					this.txtUserName.Text = "";
					this.txtPassWord.Text = "";
					if(Request.Cookies["UserName"] != null && Request.Cookies["PassWord"] != null)
					{
						Response.Cookies.Set(Request.Cookies["UserName"]);
						Response.Cookies.Set(Request.Cookies["PassWord"]);
						if (!Response.Cookies["UserName"].Value.Equals("") && !Response.Cookies["PassWord"].Value.Equals(""))
						{
							Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(1);
							Response.Cookies["PassWord"].Expires = DateTime.Now.AddMonths(1);
							Response.Cookies["UserName"].Value = strUserName;
							Response.Cookies["PassWord"].Value = strPassWord;
						}
					}
					Response.Write("<script>alert('Sửa thành công bản ghi');</script>");
					LoadUser(intUserID);
				}
				else
				{
					this.lblMess.Text = "Lỗi trong quá trình thêm mới bản ghi!";
				}
			}
		}
	}
	// Dat lai gia tri form
	private void ResetForm() 
	{
		LoadUser(intUserID);
	}
    protected void btnInsert_Click1(object sender, EventArgs e)
    {
        UpdateUserAccount();
    }
    protected void btnCancel_Click1(object sender, EventArgs e)
    {
        ResetForm();
    }
}
